const baseURL = "https://service.haixinghr.cn"
const getStatelessCheckParams = require('./getStatelessCheckParams')

const httpGet = ({url, data, isLoding, lodingMsg, contentType, header}) => {
  if(isLoding){
    lodingMsg = lodingMsg || '加载中'
    wx.showLoading({title : lodingMsg, mask : true})
  }
  let setheader = header || {}
  let setcontentType = contentType || {"Content-Type": "application/json"}
  let heders = Object.assign(setcontentType,getStatelessCheckParams(url),setheader)
  return new Promise((resovle,reject) => {
    const obj = {
      url: baseURL + url,
      data,
      header: heders,
      method: "GET",
      success: res => {
        res.statusCode === 200 ? resovle(res.data) : reject(res.data)
        if(res.data.errorCode == '04' || res.data.errorCode == '01'){
          wx.showToast({
            icon: 'none',
            duration: 2000,
            title: res.data.message
          })
        }else{
          if(isLoding){
            setTimeout(() => {
              wx.hideLoading()
            },500)
          }
        }
      },
      fail: err => {
        reject(err)
      },
      complete: () => {
        // if(isLoding){
        //   setTimeout(() => {
        //     wx.hideLoading()
        //   },1500)
        // }
      }
    }
    wx.request(obj)
  })
}

const httpPost = ({url, data, isLoding, lodingMsg, contentType, header}) => {
  if(isLoding){
    lodingMsg = lodingMsg || '加载中'
    wx.showLoading({title : lodingMsg, mask : true})
  }
  let setheader = header || {}
  let setcontentType = contentType || {"Content-Type": "application/json"}
  let heders = Object.assign(setcontentType,getStatelessCheckParams(url),setheader)
  return new Promise((resovle,reject) => {
    const obj = {
      url: baseURL + url,
      data,
      header: heders,
      method: "POST",
      success: res => {
        res.statusCode === 200 ? resovle(res.data) : reject(res.data)
        if(res.data.errorCode == '04' || res.data.errorCode == '01'){
          wx.showToast({
            icon: 'none',
            duration: 2000,
            title: res.data.message
          })
        }else{
          if(isLoding){
            setTimeout(() => {
              wx.hideLoading()
            },500)
          }
        }
      },
      fail: err => {
        reject(err)
      },
      complete: () => {
        // if(isLoding){
        //   setTimeout(() => {
        //     wx.hideLoading()
        //   },1500)
        // }
      }
    }
    wx.request(obj)
  })
}

module.exports =  {
  httpGet,
  httpPost
}